package com.hrm.dao.Impl;

import java.util.HashSet;
import java.util.List;

import com.hrm.dao.DocumentDao;
import com.hrm.domain.Document;
import com.hrm.domain.DocumentExt;
import com.hrm.utils.JDBCTools;

/*
 * 文件数据处理层实现类
 */
@SuppressWarnings("all") // 使用注解抑制警告信息
public class DocumentDaoImpl implements DocumentDao {

	// 定义全局变量
	private Class dmClass = Document.class;
	private Class dmeClass = DocumentExt.class;

	// 获取全部文件信息
	@Override
	public List<DocumentExt> getAllDocuExt() {
		String sql = "select d.`ID` fileID, d.`TITLE` fileTitle, d.`filename` fileName, d.`filetype` fileType, d.`filebytes` fileBytes, d.`REMARK` fileRemark, d.`CREATE_DATE` fileCreateDate, d.`USER_ID` fileUID,u.username dUserName from document_inf d join user_inf u on d.USER_ID=u.ID;";
		return JDBCTools.dataQuery(dmeClass, sql);
	}

	// 分页 + 模糊
	@Override
	public List<DocumentExt> getDocumentExtsByTerm(HashSet<Integer> flag, Object... parametes) {
		StringBuffer sql = new StringBuffer(
				"select d.`ID` fileID, d.`TITLE` fileTitle, d.`filename` fileName, d.`filetype` fileType, d.`filebytes` fileBytes, d.`REMARK` fileRemark, d.`CREATE_DATE` fileCreateDate, d.`USER_ID` fileUID,u.username dUserName from document_inf d join user_inf u on d.USER_ID=u.ID");
		if (!flag.contains(1)) { // 有查询条件
			sql.append(" where d.TITLE like " + parametes[0] + " and d.REMARK like " + parametes[1]
					+ " and d.filename like " + parametes[2]);
		}
		if (flag.contains(2)) { // 有用户ID
			sql.append(" and d.USER_ID = " + parametes[3]);
		}
		if (flag.contains(1)) { // 没有查询条件
			sql.append(" limit " + parametes[0] + "," + parametes[1] + ";");
		} else { // 有查询条件
			sql.append(" limit " + parametes[4] + "," + parametes[5] + ";");
		}
		return JDBCTools.dataQuery(dmeClass, sql.toString());
	}

	// 获取查询结果的记录条数
	@Override
	public Integer getCount(HashSet<Integer> flag, Object... parametes) {
		StringBuffer sql = new StringBuffer("select count(*) from document_inf d join user_inf u on d.USER_ID=u.ID");
		if (!flag.contains(1)) { // 有查询条件
			sql.append(" where d.TITLE like " + parametes[0] + " and d.REMARK like " + parametes[1]
					+ " and d.filename like " + parametes[2]);
		}
		if (flag.contains(2)) { // 有用户ID
			sql.append(" and d.USER_ID = " + parametes[3]);
		}
		sql.append(";");
		return JDBCTools.dataCount(sql.toString());
	}

	// 文件上传
	@Override
	public Integer addDocument(Document newDocument) {
		String sql = "insert into document_inf (TITLE,filename,filetype,filebytes,REMARK,USER_ID) values (?,?,?,?,?,?);";
		return JDBCTools.dataUpdate(sql, newDocument.getFileTitle(), newDocument.getFileName(),
				newDocument.getFileType(), newDocument.getFileBytes(), newDocument.getFileRemark(),
				newDocument.getFileUID());
	}

	// 根据文件ID获取指定文件的信息
	@Override
	public Document getDocumentByID(int ID) {
		Document docu = null;
		String sql = "select `ID` fileID, `TITLE` fileTitle, `filename` fileName, `filetype` fileType, `filebytes` fileBytes, `REMARK` fileRemark, `CREATE_DATE` fileCreateDate, `USER_ID` fileUID from document_inf where `ID`=?;";
		List<Document> docus = JDBCTools.dataQuery(dmClass, sql, ID);
		if (!docus.isEmpty()) {
			docu = docus.get(0);
		}
		return docu;
	}

	// 根据文件ID删除指定文件
	@Override
	public Integer delDocument(int ID) {
		String sql = "delete from `document_inf` where `ID` = ?;";
		return JDBCTools.dataUpdate(sql, ID);
	}

	// 修改根据文件ID修改指定文件的信息
	@Override
	public Integer modifyDocumentByID(Document docu, int ID) {
		String sql = "UPDATE `document_inf` SET `TITLE` = ?, `filename` = ?, `REMARK` = ? WHERE `ID` = ?;";
		return JDBCTools.dataUpdate(sql, docu.getFileTitle(), docu.getFileName(), docu.getFileRemark(), ID);
	}

}
